package com.retail.wxchat.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.core.entity.result.Result;
import com.retail.wxchat.entity.vo.RetailWxGetMoneyVo;
import com.retail.core.entity.ParamReq;
import com.retail.wxchat.entity.request.RetailWxGetMoneyRequest;
import com.retail.wxchat.mapper.RetailWxGetMoneyMapper;
import com.retail.wxchat.service.RetailWxGetMoneyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 提现手续比例 Service impl
 * @author Administrator
 */
@Service
public class RetailWxGetMoneyServiceImpl implements RetailWxGetMoneyService {

    @Resource
    private RetailWxGetMoneyMapper retailWxGetMoneyMapper;

    @Override
    public RetailWxGetMoneyVo getModel(Long id) {
        return  retailWxGetMoneyMapper.getModel(id);
    }

    @Transactional
    @Override
    public Long save(RetailWxGetMoneyRequest retailWxGetMoneyRequest) {
        retailWxGetMoneyMapper.save(retailWxGetMoneyRequest);
        return retailWxGetMoneyRequest.getId();
    }

    @Transactional
    @Override
    public Long update(RetailWxGetMoneyRequest retailWxGetMoneyRequest) {
        retailWxGetMoneyMapper.update(retailWxGetMoneyRequest);
        return retailWxGetMoneyRequest.getId();
    }

    @Override
    public List<RetailWxGetMoneyVo> listPage(ParamReq<RetailWxGetMoneyRequest> params) {
        params.setStartIndex((params.getCurrent() -1 ) * params.getSize());
        return retailWxGetMoneyMapper.listPage(params);
    }

    @Override
    public Integer countPage(ParamReq<RetailWxGetMoneyRequest> params) {
        return retailWxGetMoneyMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return retailWxGetMoneyMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public Result<Integer> insertRetailWxGetMoneyContent(RetailWxGetMoneyRequest request) {
        RetailWxGetMoneyVo retailWxGetMoneyVo = retailWxGetMoneyMapper.getUseModel();
        if (ObjectUtil.isNotEmpty(retailWxGetMoneyVo)) {
            request.setId(retailWxGetMoneyVo.getId());
            update(request);
        } else {
            request.setDelFlag(DelFlagEnum.NO.getValue());
            request.setStatusFlag(StatusFlagEnum.USE.getValue());
            save(request);
        }
        return Result.success(1);
    }

    @Override
    public Result<RetailWxGetMoneyVo> getUseRetailWxGetMoney() {
        RetailWxGetMoneyVo retailWxGetMoneyVo = retailWxGetMoneyMapper.getUseModel();
        if (ObjectUtil.isEmpty(retailWxGetMoneyVo)) {
            retailWxGetMoneyVo = new RetailWxGetMoneyVo();
        }
        return Result.success(retailWxGetMoneyVo);
    }
}
